package com.wanmait.movie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.wanmait.movie.entity.*;
import com.wanmait.movie.entity.VO.OrderDTO;
import com.wanmait.movie.entity.VO.OrdersDetailsDTO;
import com.wanmait.movie.mapper.OrderDetailsMapper;
import com.wanmait.movie.mapper.OrdersMapper;
import com.wanmait.movie.mapper.ScreeningMapper;
import com.wanmait.movie.service.OrdersService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {
    @Resource
    private OrdersMapper ordersMapper;
    @Resource
    private OrderDetailsMapper orderDetailsMapper;

    @Override
    public PageInfo<OrderDTO> getOrderList(Integer pageNum, Integer userId, Integer screeningId, Integer cinemaId, Integer lobbyId) {
        PageHelper.startPage(pageNum, 10);
        MPJLambdaWrapper<Orders> lambdaWrapper = new MPJLambdaWrapper<>();
        lambdaWrapper.select(Orders::getId, Orders::getOrderNumber, Orders::getUserId,
                        Orders::getScreeningId, Orders::getOrderTime,
                        Orders::getPayment, Orders::getMoney, Orders::getState)
                .select(Cinema::getId,Cinema::getCinemaName)
                .select(Lobby::getId,Lobby::getLobbyName)
                .select(User::getUsername)
                .select(Movie::getMovieName)
                .leftJoin(Screening.class, Screening::getId, Orders::getScreeningId)
                .leftJoin(Lobby.class, Lobby::getId, Screening::getLobbyId)
                .leftJoin(Cinema.class, Cinema::getId, Lobby::getCinemaId)
                .leftJoin(Movie.class, Movie::getId, Screening::getMovieId)
                .leftJoin(User.class, User::getId, Orders::getUserId);
        if (userId != 0) {
            lambdaWrapper.eq(Orders::getUserId, userId);
        }
        if (screeningId != 0) {
            lambdaWrapper.eq(Orders::getScreeningId, screeningId);
        }
        if (cinemaId != 0) {
            lambdaWrapper.eq(Cinema::getId, cinemaId);
            if (lobbyId != 0) {
                lambdaWrapper.eq(Lobby::getId, lobbyId);
            }
        }
        return new PageInfo<>(ordersMapper.selectJoinList(OrderDTO.class, lambdaWrapper));
    }

    @Override
    public List<OrdersDetailsDTO> getOrdersDetailsListByOrderNumber(String orderNumber) {
        MPJLambdaWrapper<OrderDetails> lambdaWrapper = new MPJLambdaWrapper<>();
        lambdaWrapper.select(OrderDetails::getMoney).select(Seat::getSeatNumber)
                .leftJoin(Seat.class, Seat::getId, OrderDetails::getSeatId)
                .eq(OrderDetails::getOrderId, orderNumber);
        return orderDetailsMapper.selectJoinList(OrdersDetailsDTO.class, lambdaWrapper);
    }

    @Override
    public List<OrderDTO> getUserOrdersList(User user) {
        MPJLambdaWrapper<Orders> lambdaWrapper = new MPJLambdaWrapper<>();
        lambdaWrapper.select(Orders::getId, Orders::getOrderNumber, Orders::getUserId,
                Orders::getScreeningId, Orders::getOrderTime,
                Orders::getPayment, Orders::getMoney, Orders::getState)
                .select(Screening::getStartTime)
                .select(Cinema::getId,Cinema::getCinemaName)
                .select(Lobby::getId,Lobby::getLobbyName)
                .select(User::getUsername)
                .select(Movie::getMovieName,Movie::getMoviePic)
                .leftJoin(Screening.class, Screening::getId, Orders::getScreeningId)
                .leftJoin(Lobby.class, Lobby::getId, Screening::getLobbyId)
                .leftJoin(Cinema.class, Cinema::getId, Lobby::getCinemaId)
                .leftJoin(Movie.class, Movie::getId, Screening::getMovieId)
                .leftJoin(User.class, User::getId, Orders::getUserId)
                .eq(Orders::getUserId, user.getId());
        List<OrderDTO> ordersList = ordersMapper.selectJoinList(OrderDTO.class, lambdaWrapper);
        return ordersList;
    }
}
